package com.yuncool.controller.v1;

import com.yuncool.auth.utils.SessionUser;
import com.yuncool.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.SocketException;

/**
 * @author ：chengccn
 * @date ：Created in 2019/8/14 17:04
 * @description：上传文件
 */
@RequestMapping("file")
@RestController
@Api(tags = "OSS上传接口")
public class UploadController {
    @Resource
    UploadService uploadService;

    @ApiOperation(value = "图片上传接口", httpMethod = "POST")
    @RequestMapping(value = "uploadImg")
    public String upload(MultipartFile file) throws IOException {
        String userId = new SessionUser().getUserDetail().getUserId();
        String userName = new SessionUser().getUserDetail().getUsername();
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件输入流
        String imageUrl = uploadService.uplaod(fileName, file.getInputStream(), userId, userName);
        return imageUrl;
    }
}